﻿using System;
using System.Collections.Generic;
using System.Web;

namespace TSharp.Core.Web.Message.MessageImpl
{
    /// <summary>
    /// 消息注册管理类
    /// <para>by tangjingbo at 2009/12/14 21:39</para>
    /// </summary>
    internal static class MessageListenerManage
    {
        public static List<IMessageListenerRegHandle<TMessage>> GetHandlers<TMessage>()
        {
            return Manage<TMessage>.Current.Get();
        }

        public static void AddHandler<TMessage>(MessageListenerRegHandleImpl<TMessage> handle)
        {
            Manage<TMessage>.Current.Add(handle);
        }

        public static void RemoveHandler<TMessage>(MessageListenerRegHandleImpl<TMessage> handle)
        {
            Manage<TMessage>.Current.Remove(handle);
        }

        #region Nested type: Manage

        private class Manage<TMessage>
        {
            private static readonly Type MsgType = typeof (TMessage);
            private static readonly Manage<TMessage> This = new Manage<TMessage>();

            private static string GetKey
            {
                get { return "_tjb.msg." + MsgType.FullName; }
            }

            public static Manage<TMessage> Current
            {
                get { return This; }
            }

            private List<IMessageListenerRegHandle<TMessage>> List
            {
                get
                {
                    var cur = (List<IMessageListenerRegHandle<TMessage>>) HttpContext.Current.Items[GetKey];
                    if (cur == null)
                    {
                        HttpContext.Current.Items[GetKey] = cur = new List<IMessageListenerRegHandle<TMessage>>();
                    }

                    return cur;
                }
                set { HttpContext.Current.Items[GetKey] = value; }
            }

            public bool Add(IMessageListenerRegHandle<TMessage> handle)
            {
                List<IMessageListenerRegHandle<TMessage>> list = List;
                if (handle.Order == int.MaxValue)
                {
                    //if (_list.Count > 0)
                    //    handle.Order = _list[_list.Count - 1].Order + 1;
                    //else
                    //{
                    //    handle.Order = 0;
                    //}
                    list.Add(handle);
                }
                else
                {
                    list.Add(handle);
                    list.Sort((x, y) => x.Order - y.Order);
                }
                return handle.Registered;
            }

            internal void Remove(IMessageListenerRegHandle<TMessage> handle)
            {
                List.Remove(handle);
            }

            public List<IMessageListenerRegHandle<TMessage>> Get()
            {
                //   System.Web.UI.WebControls.TreeNode
                return List;
            }
        }

        #endregion
    }
}